package cn.gov.service.impl;

import cn.geracomium.util.encrypt.EncryptUtil;
import cn.gov.dao.IEmployeeDAO;
import cn.gov.dao.IPersonalAccountDAO;
import cn.gov.dao.IPersonalYunDAO;
import cn.gov.service.IAccountManageService;
import cn.gov.vo.Employee;
import cn.gov.vo.PersonalAccount;
import cn.gov.vo.PersonalYun;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AccountManageServiceImpl implements IAccountManageService {

    @Autowired
    private IPersonalYunDAO personalYunDAO;
    @Autowired
    private IEmployeeDAO employeeDAO;
    @Autowired
    private IPersonalAccountDAO personalAccountDAO ;


    public Long add(PersonalYun personalYun, Employee employee) {
        //云账户信息
        PersonalAccount pa = new PersonalAccount() ;
        //激活
        pa.setMobile(personalYun.getPhone());
        pa.setStatus(1);
        pa.setCtime(new Date());
        pa.setPwd(EncryptUtil.encrypt("123456"));
        //获取上传图片URL
        pa.setPortrait(employee.getPortrait());
        personalAccountDAO.doCreateAndGet(pa) ;
        personalYun.setAccountId(pa.getAccountId());
        if (personalYun.getPersonalType() == null) {
            personalYun.setPersonalType(1);
        }
        if (personalYunDAO.doCreate(personalYun)) {
            employee.setId(pa.getAccountId());
            if(employeeDAO.doCreate(employee)){
                return pa.getAccountId() ;
            }
        }
//            employee.setId(personalYun.getId());
//            return employeeDAO.doInsert(employee) ;
        return null;
    }

    @Override
    public boolean editInitPassword(Long accountId) {
        return personalAccountDAO.doUpdateInitPassword(accountId);
    }

    @Override
    public Map<String, Object> listPre(String name, Integer personal_type, Integer status, Date begin_create_time,
                                       Date end_create_date, Integer start, Integer lineSize) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (name != null && (!"".equals(name))) {
            name = "%" + name + "%";
        }
        paramMap.put("name", name);
        paramMap.put("personal_type", personal_type);
        paramMap.put("status", status);
        paramMap.put("begin_create_time", begin_create_time);
        paramMap.put("end_create_date", end_create_date);
        paramMap.put("start", (start - 1) * lineSize);
        paramMap.put("lineSize", lineSize);
        System.out.println(personal_type);
        paramMap.put("personal_type", personal_type);


        System.out.println(paramMap);
        List<PersonalYun> allPersons = personalYunDAO.findCondition(paramMap);
        Long totalCount = personalYunDAO.getAllCount(paramMap);
        result.put("allAccounts", allPersons);
        result.put("totalCount", totalCount);
        return result;
    }

}
